import React from 'react'
import type { FormProps } from 'antd';
import { Button, Checkbox, Form, Input } from 'antd';
import { useNavigate } from 'react-router-dom';
import http from '../../utils/http'


type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};

const Login = () => {
const nav=useNavigate()
const onFinish: FormProps<FieldType>['onFinish'] =async (values) => {
  console.log('Success:', values);
  // 请求登录接口
  const res=await http.post("/api/login",{account:values.username,password:values.password})
  // 存储
  localStorage.setItem("name",res.data.username)
  localStorage.setItem("token",res.data.token)
  // 登录成功,跳转首页
  nav("/")
};
  return (
    <div>
    <Form
    name="basic"
    labelCol={{ span: 8 }}
    wrapperCol={{ span: 16 }}
    style={{ maxWidth: 600 }}
    initialValues={{ remember: true }}
    onFinish={onFinish}
    autoComplete="off"
  >
    <Form.Item<FieldType>
      label="用户名"
      name="username"
      rules={[{ required: true, message: '请输入用户名' }]}
    >
      <Input />
    </Form.Item>

    <Form.Item<FieldType>
      label="密码"
      name="password"
      rules={[{ required: true, message: '请输入密码' }]}
    >
      <Input.Password />
    </Form.Item>

    <Form.Item<FieldType> name="remember" valuePropName="checked" label={null}>
      <Checkbox>Remember me</Checkbox>
    </Form.Item>

    <Form.Item label={null}>
      <Button type="primary" htmlType="submit">
        登录
      </Button>
    </Form.Item>
  </Form>
    </div>
  )
}

export default Login
